﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Clinica_Frba.Generar_Receta
{
    public partial class AgregarMedicamento : Form
    {
        private Boolean accepted = false;
        public DataGridViewRowCollection DataGridViewRows { set; get; }

        public AgregarMedicamento()
        {
            InitializeComponent();
        }

        public static DataTable LoadDataTable()
        {

            string comando = "select m.id_med as Id, m.descripcion as Nombre from MEDICAMENTO m where m.descripcion is not null order by m.descripcion;";
            System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(publico.ConnectionString);
            myConnection.Open();
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(comando, myConnection);
            DataTable dt = new DataTable();
            
            da.Fill(dt);

            return dt;
        }

        public static AutoCompleteStringCollection LoadAutoComplete()
        {
            DataTable dt = LoadDataTable();

            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();

            foreach (DataRow row in dt.Rows)
            {
                stringCol.Add(Convert.ToString(row["Nombre"]));
            }

            return stringCol;
        }

        private void AgregarMedicamento_Load(object sender, EventArgs e)
        {
            //
            // Cargo los datos del combobox
            //
            comboBox1.DataSource = LoadDataTable();
            comboBox1.DisplayMember = "Nombre";
            comboBox1.ValueMember = "Id";

            //
            // cargo la lista de items para el autocomplete
            //
            comboBox1.AutoCompleteCustomSource = LoadAutoComplete();
            comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

            comboBox1.SelectedItem = "Seleccione un Medicamento";
            comboBox1.SelectedIndex = -1;

            comboBox2.SelectedItem = "Seleccione una cantidad";
            comboBox2.SelectedIndex = -1;
        }

        internal string GetMedicamentoSeleccionado()
        {
            /*
            Dialogos.MostrarMessageDialog("Selected value " + comboBox1.SelectedValue);
            Dialogos.MostrarMessageDialog("Selected text " + comboBox1.SelectedText);
            Dialogos.MostrarMessageDialog("Selected index " + comboBox1.SelectedIndex);
            Dialogos.MostrarMessageDialog("Selected item " + comboBox1.SelectedItem);
            Dialogos.MostrarMessageDialog("Display member " + comboBox1.DisplayMember);
            Dialogos.MostrarMessageDialog("Selected item to String " + comboBox1.SelectedItem.ToString());
            Dialogos.MostrarMessageDialog("comboBox1.GetItemText(comboBox1.SelectedItem) " + comboBox1.GetItemText(comboBox1.SelectedItem)); ;
            */

            return comboBox1.GetItemText(comboBox1.SelectedItem);
        }

        internal Boolean isAccepted()
        {
            return accepted;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                Dialogos.MostrarMessageDialog("Debe seleccionar un medicamento");
                return;
            }

            if (comboBox2.SelectedIndex == -1)
            {
                Dialogos.MostrarMessageDialog("Debe seleccionar una cantidad");
                return;
            }

            string seleccionado = GetMedicamentoSeleccionado();

            Boolean elementoEncontrado = false;
            foreach (DataGridViewRow item in DataGridViewRows)
            {
                if (((string)item.Cells["Nombre"].Value) == seleccionado)
                {
                    elementoEncontrado = true;
                    break;
                }

            }

            if (elementoEncontrado)
            {
                Dialogos.MostrarMessageDialog("El medicamento fue agregado previamente");
                return;
            }

            accepted = true;

            Close();
        }

        internal int GetIdSeleccionado()
        {
            return (int) comboBox1.SelectedValue;
        }

        internal string GetCantidadSeleccionada()
        {
            return (string)comboBox2.SelectedItem;
        }
    }


}
